// runLoaders参数options
const options = {
    // 要加载的资源
    resource: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/src/index.js",
    loaders: [ //需要加载的loader
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/post-loader1",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/post-loader2",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/inline-loader1",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/inline-loader2",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/normal-loader1",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/normal-loader2",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/pre-loader1",
      "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/pre-loader2",
    ],
    context: { //上下文，保存一些状态和值 
      name: "ikki",
      age: 100,
    },
    readResource: fs.readFile.bind(this), //读写文件的方法
  }

// loaderContext上下文，也是最关键的一环，后续在loader中调用的this就是loaderContext
const loaderContext =   {
    name: "ikki", 
    age: 100,
    resource: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/src/index.js", //要加载的资源
    readResource: fs.readFile.bind(this), //读取资源的方法
    loaders: [ //所有的loader对象
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/post-loader1",
        normal: function loader(source){
          console.log('post1')
          return source + "//post1"
        },
        pitch: function(){
          console.log('post1 pitch')
        },
        raw: undefined, //是否是buffer
        data: {}, //每个loader都可以携带一个自定义data对象
        pitchExecuted: false, //此loader的pitch函数是否已经 执行过
        normalExecuted: false, //此loader的normal函数是否已经执行过
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/post-loader2",
        normal: function loader(source){
          console.log('post2')
          return source + "//post2"
        },
        pitch: function(){
          console.log('post2 pitch')
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/inline-loader1",
        normal: function loader(source){
          console.log('inline1')
          
          return source + "//inline1"
        },
        pitch: function(remainingRequest,previousRequest,data){
          // console.log('inline1-pitch')
          console.log('remainingRequest',remainingRequest);
          console.log('previousRequest',previousRequest);
          console.log('data',data);
          console.log('inline1 pitch')
          return 'ok'
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/inline-loader2",
        normal: function loader(source){
          console.log('inline2')
          return source + "//inline2"
        },
        pitch: function(){
          console.log('inline2 pitch')
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/normal-loader1",
        normal: function loader(source){
          console.log('normal1')
          return source + "//normal1"
        },
        pitch: function(){
          console.log('normal1 pitch')
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/normal-loader2",
        normal: function loader(source){
          console.log('normal2')
          return source + "//normal2"
        },
        pitch: function(){
          console.log('normal2 pitch')
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/pre-loader1",
        normal: function loader(source){
          console.log('pre1')
          return source + "//pre1"
        },
        pitch: function(){
          console.log('pre1 pitch')
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
      {
        path: "/Users/adminx/Desktop/Ikki-study/webpack-advanced/webpack5-base/5.loader/doc/02.手写loader-runner/loaders-chain/pre-loader2",
        normal: function loader(source){
          console.log('pre2')
          // 同步编变成异步
          // 当次函数执行完成以后，不会立即执行下一个loader，而是会等待执行完毕再执行下一个loader
          // let callback = this.async()
          // setTimeout(() => {
          //     callback(null,source+'//pre2')
          // }, 1000);
          return source + "//pre2"
        },
        pitch: function(remainingRequest,previousRequest,data){
          
          console.log('pre2 pitch')
        },
        raw: undefined,
        data: {
        },
        pitchExecuted: false,
        normalExecuted: false,
      },
    ],
    loaderIndex: 0, //当前正在执行的loader索引
    callback: null, //回调
    async: null,//把loader的执行从同步变成异步，当次loader函数执行完成以后，不会立即执行下一个loader，而是会等待异步执行完毕再执行下一个loader
    request:{
        get(){ //聚合loader，并且加上resouce ===>   （loader1!loader2!loader3!index.js）
            return loaderContext.loaders.map(loader=>loader.path).concat(loaderContext.resource).join('!')
        }
        
    },
    remainingRequest:{ 
        get(){  //获取从下一个loader开始一直到结束的loader ，加上要加载的资源
            return loaderContext.loaders.slice(loaderContext.loaderIndex + 1).map(loader => loader.path).concat(loaderContext.resource).join('!');
        }
    },
    currentRequest:{
        get() { //从当前的loader开始一直到结束 ，加上要加载的资源
            return loaderContext.loaders.slice(loaderContext.loaderIndex).map(loader => loader.path).concat(loaderContext.resource).join('!');
        }
    },
    previousRequest:{
        get() { //从第一个到当前的loader的前一个
            return loaderContext.loaders.slice(0, loaderContext.loaderIndex).map(loader => loader.path).join('!');
        }
    },
    data:{
        get() { // 当前loader中的自定义data
            return loaderContext.loaders[loaderContext.loaderIndex]?.data;
        }
    }
  }

